import { BaseModel, sequelize, Sequelize } from "./basemodel.js";
import Users from "./users.js";

class EmployerApplications extends BaseModel {
	static init() {
		return super.init(
			{
				application_id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
				company_name: { type: Sequelize.STRING(100), allowNull: false },
				business_license: { type: Sequelize.STRING(50), allowNull: false, unique: true },
				contact_person: { type: Sequelize.STRING(50), allowNull: false },
				contact_phone: { type: Sequelize.STRING(20), allowNull: false },
				contact_email: { type: Sequelize.STRING(100), allowNull: false },
				company_address: { type: Sequelize.STRING(200), allowNull: false },
				company_website: { type: Sequelize.STRING(200), allowNull: true },
				application_status: { 
					type: Sequelize.ENUM('pending', 'approved', 'rejected'),
					defaultValue: 'pending',
					allowNull: false 
				},
				reviewer_id: { 
					type: Sequelize.INTEGER,
					allowNull: true,
					references: {
						model: 'users',
						key: 'user_id'
					}
				},
				review_time: { type: Sequelize.DATE, allowNull: true },
				review_notes: { type: Sequelize.TEXT, allowNull: true },
				created_at: { type: Sequelize.DATE, allowNull: false },
				updated_at: { type: Sequelize.DATE, allowNull: false }
			},
			{
				sequelize,
				tableName: "employer_applications",
				modelName: "employer_applications",
				timestamps: true,
				createdAt: 'created_at',
				updatedAt: 'updated_at'
			}
		);
	}

	static associate() {
		this.belongsTo(Users, {
			foreignKey: 'reviewer_id',
			as: 'reviewer'
		});
	}

	static listFields() {
		return [
			'application_id',
			'company_name',
			'business_license',
			'contact_person',
			'contact_phone',
			'contact_email',
			'company_address',
			'company_website',
			'application_status',
			'created_at',
			'updated_at',
			'reviewer_id',
			'review_time',
			'review_notes'
		];
	}

	static viewFields() {
		return this.listFields();
	}

	static editFields() {
		return [
			'application_id',
			'company_name',
			'business_license',
			'contact_person',
			'contact_phone',
			'contact_email',
			'company_address',
			'company_website',
			'application_status',
			'reviewer_id',
			'review_time',
			'review_notes'
		];
	}

	static searchFields() {
		return [
			Sequelize.literal("company_name LIKE :search"),
			Sequelize.literal("business_license LIKE :search"),
			Sequelize.literal("contact_person LIKE :search"),
			Sequelize.literal("contact_phone LIKE :search"),
			Sequelize.literal("contact_email LIKE :search"),
			Sequelize.literal("application_status LIKE :search")
		];
	}
}

EmployerApplications.init();
EmployerApplications.associate();
export default EmployerApplications; 